//
//  HJEmoticonToolBar.swift
//  Weibo
//
//  Created by 韩佳 on 16/4/26.
//  Copyright © 2016年 ddg. All rights reserved.
//

import UIKit

enum HJEmoticonType:Int {
    case Rencent = 0,Default,Emoji,Lxh
}

class HJEmoticonToolBar: UIStackView {

    private var lastSelectedBtn:UIButton?
    var emoticonButtonClickClosure:((type: HJEmoticonType)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        distribution = .FillEqually
        addChildButton("最近", imageName: "compose_emotion_table_left", type: HJEmoticonType.Rencent)
        addChildButton("默认", imageName: "compose_emotion_table_mid", type: HJEmoticonType.Default)

        addChildButton("Emoji", imageName: "compose_emotion_table_mid", type: HJEmoticonType.Emoji)

        addChildButton("浪小花", imageName: "compose_emotion_table_right", type: HJEmoticonType.Lxh)
    }
    private func addChildButton(title:String, imageName:String,type: HJEmoticonType){
        let button = UIButton()
        button.addTarget(self, action: "childButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.tag = type.rawValue
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        button.setBackgroundImage(UIImage(named: "\(imageName)_normal"), forState: UIControlState.Normal)
            button.setBackgroundImage(UIImage(named: "\(imageName)_selected"), forState: UIControlState.Selected)
        addArrangedSubview(button)
    }
    @objc private func childButtonClick(sender:UIButton){
        
        if lastSelectedBtn == sender{
            return
        }
        lastSelectedBtn?.selected = false
        sender.selected = true
        lastSelectedBtn = sender
        emoticonButtonClickClosure?(type: HJEmoticonType(rawValue: sender.tag)!)
    }
}







